var express = require('express');
var router = express.Router();
var utils = require('../functions/utils');


router.get('/:encryptedId', function(req,res)
{
/*
	// maintenance
	var maintenance_content='<html><body><h2><br><br><center> We are so sorry.<br>We are under maintenance for 1 hour.</center></body></html>';
	res.end(maintenance_content);
	return;
*/

	utils.callLog(req);

	var encArticleId = req.params.encryptedId;

	var async = require('async');
	var dbData = null;
	var state = null;
	var articleId = null;
	var keyword = '';

	async.series(
		[
			// db 
			function(CB)
			{
				// 읽는건 replica 
				global.replica_db.connection.getConnection(function(err,connection)
				{
					if(err) { throw err; }

					var profileImageSizeId = 2; // 프로파일 이미지 작은거
					var thumbnailSizeId = 2; // 섬네일 이미지 작은거 

					var input_param = [encArticleId 
							, global.config.web.appId , global.config.web.appVersion 
							, profileImageSizeId , thumbnailSizeId ,global.config.storage.download.url , global.config.storage.download.secretUrl];

					var sql = 'call api_getSingleArticleViewPageInfoWithKeyword(? , ? , ? , ? , ? , ? , ?, @articleId , @state , @webKeyword , @tagList ); ';
					sql += 'SELECT @articleId as articleId ,  @state as state , @webKeyword as webKeyword , @tagList as tagList ';

					var query = connection.query(sql, input_param , function (err, rows) 
					{
						connection.release();
						if(err) { throw err; }


						// 에러나 결과가 없을 경우도 화면에는 표시해줘야하므로 - 에러날리지 않음. 
						// 결과가 없을 경우 
						if((rows.length  > 2 && rows[0].length == 0) || rows.length == 2)
						{
							// error
							CB("error");
						}
						else
						{
							dbData = rows[0][0];

							// next step
							CB(null);
						}

					}); // call api_incShareCount

				}); // getConnection
	
			}
			// read tempate 
			,function(CB)
			{
				// 기존에 없을 경우만 읽는다
				if(global.snsShareData_template == null)
				{
					var fs = require('fs');
					fs.readFile('./template/page/snsShareData' , function(error,data)
					{
						if(error)
						{
							utils.error("loadVideoPage : template read error");
							throw err;
						}
						global.snsShareData_template = String(data);
						utils.debug('loadVideoPage : read template page');

						// next step
						CB(null);
					});
				}
				else
				{
						// next step
						CB(null);
				}

			}
		]
		,function(err, results) 
		{
			var content = global.snsShareData_template;

			// 결과있음.
			if(dbData)
			{

				if(utils.isValidString(dbData.articleBody))
				{
					content = content.split("%ARTICLE_BODY_VALUE%").join(utils.adjustPropValue(dbData.articleBody));
				}
				else
				{
					content = content.split("%ARTICLE_BODY_VALUE%").join(utils.adjustHTMLString(''));
				}


				content = content.split("%THUMB_URL%").join(utils.isValidString(dbData.thumbUrl) ? dbData.thumbUrl : '');
				content = content.split("%VIDEO_SECRET_URL%").join(utils.isValidString(dbData.videoSecretUrl) ? dbData.videoSecretUrl : '');

				content = content.split("%VIDEO_WIDTH%").join(dbData.width);
				content = content.split("%VIDEO_HEIGHT%").join(dbData.height);

				// 공통부분 - 설정에 있는 url 적어주는 부분들 
				content = content.split("%ENC_ID%").join(encArticleId); 

			}
			// 결과없음.
			else
			{
				content = 'ERROR';
			}
	
			res.writeHead(200 , {'Content-Type' : 'text/html'});
			res.end(content);

		}
	);
	
});

module.exports = router;